
package com.hyfrogx.modules.manage.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.app.form.video.SmallVideoVo;
import com.hyfrogx.modules.manage.dao.videoSmall.HyVideoSmallDao;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallEntity;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallLikeEntity;
import com.hyfrogx.modules.manage.service.HyVideoSmallLikeService;
import com.hyfrogx.modules.manage.service.HyVideoSmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HyVideoSmallServiceImpl extends ServiceImpl<HyVideoSmallDao, HyVideoSmallEntity> implements HyVideoSmallService {

	@Autowired
	HyVideoSmallLikeService hyVideoSmallLikeService;


	@Override
	public IPage getSmallVideoPage(QueryExt query) {
		Map map = new HashMap();
		map.putAll(query.getCondition());
		List<Map> list=this.baseMapper.getList(query,map);
		if(list!=null&&map.get("uid")!=null){
			//如果uid非空 判断是否点赞
			List<HyVideoSmallLikeEntity> liskeList=hyVideoSmallLikeService.getBaseMapper().selectList(
					new QueryWrapper<HyVideoSmallLikeEntity>().eq("uid",map.get("uid")));
			if(liskeList!=null&&liskeList.size()>0){
				for (Map map1 : list) {
					String id=map1.get("id").toString();
					boolean isLike=false;
					for(HyVideoSmallLikeEntity entity:liskeList){
						if(id.equals(entity.getWid())){
							isLike=true;
							break;
						}
					}
					map1.put("isLike",isLike);
				}
			}
		}
		query.setRecords(list);
		return query;
	}

	@Override
	public IPage selectBycondition(QueryExt query) {
		Map map = new HashMap();
		map.putAll(query.getCondition());
		query.setRecords(this.baseMapper.selectBycondition(query,map));
		return query;
	}

	@Override
	public void updateSort(List<HyVideoSmallEntity> list) {
		this.baseMapper.updateSort(list);
	}


	@Override
	public SmallVideoVo getDetailById(Integer id) {
		HyVideoSmallEntity result = this.baseMapper.selectById(id);
        SmallVideoVo vo=BeanUtil.toBean(result,SmallVideoVo.class);
		return vo;
	}

	@Override
	public void batchDelete(List<String> ids) {
		this.baseMapper.deleteBatchIds(ids);
	}

	@Override
	public void updateState(Integer id, Integer state) {
		HyVideoSmallEntity wuli = new HyVideoSmallEntity();
		wuli.setId(id);
		wuli.setState(state);
		if(state==1){
			wuli.setPublishTime(new Date());
		}
		this.baseMapper.updateById(wuli);
	}


	@Override
	public void clearData() {
		this.baseMapper.clearData();
	}



}
